using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Tao.OpenGl;

namespace $safeprojectname$
{

    public partial class Form1 : Form
    {
        //private Vector objectPosition;
        //private Vector objectRotation;
        //private float objectRotationAngle;
        //private Color objectColor;
        //private Vector objectScale;        

        public Form1()
        {
            InitializeComponent();
        }

        private void simpleOpenGlControl1_Load(object sender, EventArgs e)
        {
            simpleOpenGlControl1.InitializeContexts();
            simpleOpenGlControl1.AutoSwapBuffers = true;

            //objectPosition = new Vector(0.0, 0.0, 0.0);
            //objectRotation = new Vector(0.0, 0.0, 0.0);
            //objectRotationAngle = 0.0f;
            //objectScale = new Vector(1.0, 1.0, 1.0);
            //objectColor = Color.LightGoldenrodYellow;
            
            InitGL();
            SetLights();

            simpleOpenGlControl1_Resize(null, null);
        }

        private static bool InitGL()
        {
            //Gl.glShadeModel(Gl.GL_SMOOTH);
            Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            Gl.glClearDepth(1);
            Gl.glEnable(Gl.GL_DEPTH_TEST);
            Gl.glDepthFunc(Gl.GL_LEQUAL);
            Gl.glHint(Gl.GL_PERSPECTIVE_CORRECTION_HINT, Gl.GL_NICEST);
            Gl.glEnable(Gl.GL_LIGHT0);
            Gl.glEnable(Gl.GL_LIGHT1);
            Gl.glEnable(Gl.GL_LIGHTING);
            Gl.glEnable(Gl.GL_NORMALIZE);

            return true;
        }

        private void SetLights()
        {
            // global light
            float[] lmodel_ambient = { 0.5f, 0.5f, 0.5f, 1.0f };
            Gl.glLightModelfv(Gl.GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);

            float[] lightAmbient = { 0.5f, 0.5f, 0.5f, 1.0f };
            float[] lightDiffuse = { 0.4f, 0.4f, 0.4f, 1.0f };
            float[] lightPosition = { -10.0f, -10.0f, 5.0f, 1.0f };
            float[] lightPosition2 = { 10.1f, -10.0f, 10.0f, 1.0f };

            Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_POSITION, lightPosition);
            //Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_SPECULAR, lightSpecular);
            Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_AMBIENT, lightAmbient);
            Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_DIFFUSE, lightDiffuse);

            Gl.glLightfv(Gl.GL_LIGHT1, Gl.GL_POSITION, lightPosition2);
            //Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_SPECULAR, lightSpecular);
            Gl.glLightfv(Gl.GL_LIGHT1, Gl.GL_AMBIENT, lightAmbient);
            Gl.glLightfv(Gl.GL_LIGHT1, Gl.GL_DIFFUSE, lightDiffuse);
        }

        private void simpleOpenGlControl1_Resize(object sender, EventArgs e)
        {
            int w = simpleOpenGlControl1.Width;
            int h = simpleOpenGlControl1.Height;

            float ratio = ((float)w / (float)h);

            // set viewport to current window size
            Gl.glViewport(0, 0, w, h);

            // set up prespective projection
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            Glu.gluPerspective(60, ratio, 1, 50);

            // set up model view matrix
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();
        }

        private void simpleOpenGlControl1_Paint(object sender, PaintEventArgs e)
        {
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
            Gl.glLoadIdentity();
            Gl.glPushMatrix();
            Gl.glTranslatef(0.0f, 0.0f, -10.0f);
            //Gl.glTranslatef((float)objectPosition.X, (float)objectPosition.Y, (float)objectPosition.Z - 10.0f);
            //Gl.glTranslatef((float)objectPosition.X, (float)objectPosition.Y, (float)objectPosition.Z - 10.0f);
            //Gl.glRotatef(objectRotationAngle, (float)objectRotation.X, (float)objectRotation.Y, (float)objectRotation.Z);
            //Gl.glScalef((float)objectScale.X, (float)objectScale.Y, (float)objectScale.Z);

            DrawAxis();

            // ***********************
            //       Draw Here
            // ***********************

            Gl.glPopMatrix();
        }

        private void DrawAxis()
        {
            Color color = Color.LightYellow;

            float[] mat_ambient = { 0.2f * (float)color.R / 255.0f, 0.2f * (float)color.G / 255.0f, 0.2f * (float)color.B / 255.0f, 1.0f };
            float[] mat_diffuse = { 0.01f * (float)color.R / 255.0f, 0.01f * (float)color.G / 255.0f, 0.01f * (float)color.B / 255.0f, 1.0f };

            Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_AMBIENT, mat_ambient);
            Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_DIFFUSE, mat_diffuse);

            Gl.glBegin(Gl.GL_LINES);
         
            Gl.glVertex3f(0.0f, 0.0f, 0.0f);
            Gl.glVertex3f(1.0f, 0.0f, 0.0f);
            
            Gl.glEnd();


            color = Color.LightGreen;
            mat_ambient = new float[]{ 0.2f * (float)color.R / 255.0f, 0.2f * (float)color.G / 255.0f, 0.2f * (float)color.B / 255.0f, 1.0f };
            mat_diffuse = new float[]{ 0.01f * (float)color.R / 255.0f, 0.01f * (float)color.G / 255.0f, 0.01f * (float)color.B / 255.0f, 1.0f };

            Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_AMBIENT, mat_ambient);
            Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_DIFFUSE, mat_diffuse);

            Gl.glBegin(Gl.GL_LINES);

            Gl.glVertex3f(0.0f, 0.0f, 0.0f);
            Gl.glVertex3f(0.0f, 1.0f, 0.0f);

            Gl.glEnd();


            color = Color.LightSkyBlue;
            mat_ambient = new float[] { 0.2f * (float)color.R / 255.0f, 0.2f * (float)color.G / 255.0f, 0.2f * (float)color.B / 255.0f, 1.0f };
            mat_diffuse = new float[] { 0.01f * (float)color.R / 255.0f, 0.01f * (float)color.G / 255.0f, 0.01f * (float)color.B / 255.0f, 1.0f };

            Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_AMBIENT, mat_ambient);
            Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_DIFFUSE, mat_diffuse);


            Gl.glBegin(Gl.GL_LINES);

            Gl.glVertex3f(0.0f, 0.0f, 0.0f);
            Gl.glVertex3f(0.0f, 0.0f, 1.0f);

            Gl.glEnd();
        
        }

        private void startButton_Click(object sender, EventArgs e)
        {
            drawTimer.Start();
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            drawTimer.Stop();
        }

        private void resetButton_Click(object sender, EventArgs e)
        {
            drawTimer.Stop();
        }

        private void drawTimer_Tick(object sender, EventArgs e)
        {
            simpleOpenGlControl1.Refresh();
        }
    }
}